package feevale.dropbox.service;

import java.io.File;

/**
 * @author richard
 */

public class DirectoryMonitor implements Runnable {
	
	private static final Long SYNCHRONIZE = 1000L;
	
	private Share share;
	private Thread thread;
	
	public DirectoryMonitor(Share share) {
		this.share = share;
		
		this.thread = new Thread(this);
		thread.start();
	}

	@Override
	public void run() {
		
        try {
            while (true) {
            	
                File[] arquivos = share.getFolder().listFiles();
                
                for (File f : arquivos) {
                    if (!share.FILES.contains(f)) {
                        share.FILES.add(f);
                    }
                }
                
                File tmpArquivos[] = share.FILES.toArray(new File[share.FILES.size()]);
                
                for (File f : tmpArquivos) {
                    if (!(f.exists())) {
//                        share.DELETED_FILES.add(f);
                    }
                }
            	
                Thread.sleep(SYNCHRONIZE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
	}
}
